import logging.handlers
from .__remote_log__ import RemoteLogger


class LocalBufferingHandler(logging.handlers.BufferingHandler):
    '''
    本地的日志缓存

    Args:
     - capacity: 缓存的最大字节数
    '''

    def __init__(self, capacity=1000000):
        super().__init__(capacity)

    def emit(self, record):
        msg = self.format(record)
        super().emit(msg)
        RemoteLogger().send(msg)

    def close(self):
        buffer = self.buffer
        super().close()
        return buffer

    def add_buffer(self, buffer):
        '''添加日志'''
        self.acquire()
        self.buffer.append(buffer)
        RemoteLogger().send(buffer)
        self.release()
